#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int maxn = 1010;
int n, m, flag[maxn], first, ans, vis[maxn], s, a[maxn], e[maxn][maxn], rudu[maxn], stack1[maxn], top;

void init()
{
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= m; i++)
    {
        memset(flag, 0, sizeof(flag));
        scanf("%d", &s);
        for (int j = 1; j <= s; j++)
        {
            scanf("%d", &a[j]);
            flag[a[j]] = 1;
        }
        for (int j = a[1]; j <= a[s]; j++)
            if (!flag[j])
                for (int k = 1; k <= s; k++)
                    if (!e[j][a[k]])
                    {
                        e[j][a[k]] = 1;
                        rudu[a[k]]++;
                    }
    }
}

void tp()
{
    first = 1;
    while (top != 0 || first)
    {
        first = 0;
        top = 0;
        for (int i = 1; i <= n; i++)
            if (!rudu[i] && !vis[i])
            {
                stack1[++top] = i;
                vis[i] = 1;
            }
        for (int i = 1; i <= top; i++)
            for (int j = 1; j <= n; j++)
                if (e[stack1[i]][j])
                {
                    e[stack1[i]][j] = 0;
                    rudu[j]--;
                }
        ans++;
    }
    ans--;  
}

int main()
{
    init();
    tp();
    printf("%d", ans);

    return 0;
}
